const webpack = require("webpack");
const fs = require("fs");
const path = require("path");
const homedir = require("os").homedir();

const CompressionPlugin = require('compression-webpack-plugin');
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i;


let merge = require("webpack-merge");
merge = merge.merge ? merge.merge : merge;
const webpackConfig = function (dirName, feDir, options) {
    let devServerSettings = {
        https: true,
        // clientLogLevel: "warning",
        // inline: true,
        // hot: true,
        host: "my-port.thebizark.com",
        // port: 8443, // CHANGE YOUR PORT HERE!
        // open: process.platform === "darwin",
        // open: true,
        // disableHostCheck: true,
        // overlay: { warnings: false, errors: true }
    };
    const localCertPath = path.join(
        homedir,
        "Code",
        "certs",
        "thebizark.com",
        "fullchain.cer"
    );
    const localCertKeyPath = path.join(
        homedir,
        "Code",
        "certs",
        "thebizark.com",
        "ssl.key"
    );
    if(fs.existsSync(localCertPath) && fs.existsSync(localCertKeyPath)){
        devServerSettings.https = {
            cert: fs.readFileSync(localCertPath),
            key: fs.readFileSync(localCertKeyPath)
        };
    }
    return merge({
        outputDir: path.resolve(dirName, './dist'),
        assetsDir: "./static",
        lintOnSave: false,
        devServer: devServerSettings,
        publicPath: '/',
        productionSourceMap: false,
        configureWebpack: {
            performance: {
                hints: false
            },
            
            resolve: {
                extensions: [".js", ".vue", ".json"],
                modules: [path.resolve(dirName, feDir, "./node_modules"), "node_modules"],
                alias: {
                    // vue$: "vue/dist/vue.esm.js",
                    vue$: "vue/dist/vue.esm-bundler.js",
                    "@": path.resolve(dirName, "./src/"),
                    "@mods": path.resolve(dirName, feDir, "./src/"),
                    "@components": path.resolve(dirName, feDir, "./src/components/"),
                },
                // fallback: {
                //     "crypto": false 
                // }
                fallback: {
                    util: require.resolve("util/"),
                    crypto: require.resolve("crypto-browserify"),
                    stream: require.resolve("stream-browserify")
                }
            },
            resolveLoader: {
                mainFields: ['loader', 'main'],
                extensions: [".js", ".json"],
                modules: [path.resolve(dirName, feDir, "./node_modules"), "node_modules"],
            },
            plugins: [
                new CompressionPlugin({
                    // gzip压缩配置
                    test: /\.js$|\.html$|\.css/, // 匹配文件名
                    threshold: 102400, // 对超过10kb的数据进行压缩
                    deleteOriginalAssets: false, // 是否删除原文件
                }),
                new webpack.ProvidePlugin({
                    "Echarts": "echarts",
                    "process": "process/browser",
                    // "$": 'jquery',
                    // "jquery": 'jquery',
                    // "window.jQuery":'jquery'
                }),
                // new webpack.DllReferencePlugin({
                //     context: dirName,
                //     manifest: require(path.resolve(dirName, './public/dll/mainfest.json')) // 指向生成的manifest.json
                // }),
            ],

        },
        chainWebpack: config => {
            const resolveModules = config.resolve.modules;
            if (resolveModules.store instanceof Set) resolveModules.store.delete('node_modules');

            config.module
                .rule("i18n")
                .resourceQuery(/blockType=i18n/)
                .type('javascript/auto')
                .use("i18n")
                // .loader("@kazupon/vue-i18n-loader")
                .end();
            config.plugin('define')
                .tap(args => {
                    args[0]['process.env']['VUE_APP_VERSION'] = JSON.stringify(`${process.env.npm_package_version}@${Date.now()}`);//主版本+/随机号
                    return args
                });
        },
    }, options || {})
}

module.exports = webpackConfig;

